package com.spirng.leetcode.day05;

import java.util.Deque;
import java.util.LinkedList;

public class Test150 {
    public static void main(String[] args) {
        Test150 test150 = new Test150();
        test150.evalRPN(new String[]{"10","6","9","3","+","-11","*","/","*","17","+","5","+"});
    }
    public  int evalRPN(String[] tokens) {
          Deque<Integer> deque =new LinkedList<>();
        for (int i = 0; i < tokens.length; i++) {
           if(isOperator(tokens[i])){
               int j= deque.removeFirst();
               int h=deque.removeFirst();
               int computing = computing(tokens[i], h, j);
               int s =computing;
               deque.addFirst(s);
           }else {
               deque.addFirst(Integer.valueOf(tokens[i]));
           }
        }
        Integer i = deque.removeFirst();
        return i;
    }
    public  boolean isOperator(String token) {
        return token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/");
    }
    public  int computing(String s,int i,int j){
        if(s.equals("+")){
            return i+j;
        }
        if(s.equals("-")){
            return i-j;
        }
        if(s.equals("*")){
            return i*j;
        }
        if(s.equals("/")){
            return i/j;
        }
        return -1;
    }
}
